package com.itheima.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.itheima.common.BaseContext;
import com.itheima.common.R;
import com.itheima.pojo.ShoppingCart;
import com.itheima.service.ShoppingCartService;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Delete;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("shoppingCart")
public class ShoppingCartController {
    @Autowired
    private ShoppingCartService shoppingCartService;

    /**
     * <p>Description: 添加购物车</p>
     *
     * @author liuyiming
     * @date 2022-10-14 16:49
     * @version 1.0.0
     */
    @PostMapping("/add")
    public R<ShoppingCart> add(@RequestBody ShoppingCart shoppingCart){
        log.info("添加购物车");
        shoppingCart.setUserId(BaseContext.getCurrentId());
        //查询当前菜品/套餐在购物车中是否已存在
        LambdaQueryWrapper<ShoppingCart> lqw = new LambdaQueryWrapper<>();
        lqw.eq(ShoppingCart::getUserId,BaseContext.getCurrentId());
        lqw.eq(shoppingCart.getDishId() != null,ShoppingCart::getDishId,shoppingCart.getDishId());
        lqw.eq(shoppingCart.getSetmealId() != null,ShoppingCart::getSetmealId,shoppingCart.getSetmealId());
        ShoppingCart shoppingCartInfo = shoppingCartService.getOne(lqw);
        shoppingCart.setCreateTime(LocalDateTime.now());
        //如果查询出的菜品/套餐不存在，则新增，反之则对数量进行加一
        if (shoppingCartInfo == null){
            shoppingCartService.save(shoppingCart);
            shoppingCart.setNumber(1);
            shoppingCartInfo = shoppingCart;
        }else{
            shoppingCartInfo.setNumber(shoppingCartInfo.getNumber()+1);
            shoppingCartService.updateById(shoppingCartInfo);
        }
        return R.success(shoppingCartInfo);
    }

    /**
     * <p>Description: 查询当前用户购物车</p>
     *
     * @author liuyiming
     * @date 2022-10-14 18:33
     * @version 1.0.0
     */
    @GetMapping("/list")
    public R<List<ShoppingCart>> list(){
        log.info("查询当前用户的购物车");
        LambdaQueryWrapper<ShoppingCart> lqw = new LambdaQueryWrapper<>();
        lqw.eq(ShoppingCart::getUserId,BaseContext.getCurrentId());
        List<ShoppingCart> list = shoppingCartService.list(lqw);
        return R.success(list);
    }


    /**
     * <p>Description: 删除购物车中的菜品或套餐</p>
     *
     * @author liuyiming
     * @date 2022-10-14 18:33
     * @version 1.0.0
     */
    @PostMapping("/sub")
    public R<ShoppingCart> delete(@RequestBody ShoppingCart shoppingCart){
        log.info("删除购物车中的菜品或套餐");
        LambdaQueryWrapper<ShoppingCart> lqw = new LambdaQueryWrapper<>();
        lqw.eq(ShoppingCart::getUserId,BaseContext.getCurrentId());
        lqw.eq(shoppingCart.getDishId() != null,ShoppingCart::getDishId,shoppingCart.getDishId());
        lqw.eq(shoppingCart.getSetmealId() != null,ShoppingCart::getSetmealId,shoppingCart.getSetmealId());
        ShoppingCart shoppingCartInfo = shoppingCartService.getOne(lqw);
        if (shoppingCartInfo.getNumber() > 1){
            if (shoppingCart.getSetmealId()!=null){
                shoppingCartInfo.setSetmealId(shoppingCart.getSetmealId());
            }
            if (shoppingCart.getDishId()!=null){
                shoppingCartInfo.setDishId(shoppingCart.getDishId());
            }
            shoppingCartInfo.setNumber(shoppingCartInfo.getNumber()-1);
            shoppingCartService.updateById(shoppingCartInfo);
        }else{
            shoppingCartInfo.setNumber(0);
            shoppingCartService.removeById(shoppingCartInfo.getId());
        }
        return R.success(shoppingCartInfo);
    }

    /**
     * <p>Description: 清空购物车</p>
     *
     * @author liuyiming
     * @date 2022-10DeleteM1
     * @version 1.0.0
     */
    @DeleteMapping("/clean")
    public R<String> clean(){
        log.info("清空购物车");
        LambdaUpdateWrapper<ShoppingCart> luw = new LambdaUpdateWrapper<>();
        luw.eq(ShoppingCart::getUserId,BaseContext.getCurrentId());
        shoppingCartService.remove(luw);
        return R.success("清空成功");
    }


}
